package com.kolon.business.mOI.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIExecSuggestionViewController")
public class MOIExecSuggestionViewController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIExecSuggestionView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// XML URL
		String url = MsgUtil.getMessage("xml.url");
		// 파라미터
		String seq = request.getParameter("seq");
		String page = request.getParameter("page");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String cpyCD = (String)userMap.get("CompanyCD");
		// --- [심사 아이디어 제안 보기] --------------------------------------------
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=ExecView&mode=3&Seq=0");
		buffer.append("&ESeq=").append(seq);
		buffer.append("&companycd=").append(cpyCD);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardDetail");
		Iterator itXml = xmlList.iterator();
		Map map = new HashMap();
		String[] strJoinerArr = null;
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("SugID", el.getChild("SugID").getText());
			map.put("IdeaBaseSeq", el.getChild("IdeaBaseSeq").getText());
			map.put("ThemeSeq", el.getChild("ThemeSeq").getText());
			map.put("ExecEmpID", el.getChild("ExecEmpID").getText());
			map.put("ExecEmpName", el.getChild("ExecEmpName").getText());
			map.put("ExecEmpDeptCD", el.getChild("ExecEmpDeptCD").getText());
			map.put("ExecEmpDeptName", el.getChild("ExecEmpDeptName").getText());
			map.put("ExecSDate", el.getChild("ExecSDate").getText());
			map.put("ExecEDate", el.getChild("ExecEDate").getText());
			map.put("SugFlagName", el.getChild("SugFlagName").getText());
			map.put("Reson", el.getChild("Reson").getText());
			map.put("YearEffect", el.getChild("YearEffect").getText());
			map.put("ThisYearEffect", el.getChild("ThisYearEffect").getText());
			map.put("Status", el.getChild("Status").getText());
			map.put("StatusName", el.getChild("StatusName").getText());
			map.put("JudgeEmpID", el.getChild("JudgeEmpID").getText());
			map.put("JudgeEmpName", el.getChild("JudgeEmpName").getText());
			map.put("JudgeClass", el.getChild("JudgeClass").getText());
			map.put("JudgeResult", el.getChild("JudgeResult").getText());
			map.put("JudgeResultName", el.getChild("JudgeResultName").getText());
			map.put("JudgeDate", el.getChild("JudgeDate").getText());
			map.put("JudgeOpinion", el.getChild("JudgeOpinion").getText());
			map.put("ExecEmpTitleName", el.getChild("ExecEmpTitleName").getText());
			map.put("Class", el.getChild("Class").getText());
			strJoinerArr = ComUtil.nullChk(el.getChild("strJoiner").getText()).split(";");
			map.put("strJoiner", el.getChild("strJoiner").getText());
			map.put("JudgeDeptCD", el.getChild("JudgeDeptCD").getText());
			map.put("JudgeDeptName", el.getChild("JudgeDeptName").getText());
			map.put("SearchAuth", el.getChild("SearchAuth").getText());
			map.put("JedgeChangCnt", el.getChild("JedgeChangCnt").getText());
			map.put("YearEffect2", el.getChild("YearEffect2").getText());
			map.put("ThisYearEffect2", el.getChild("ThisYearEffect2").getText());
			if (el.getChild("Unsigned") != null)
			map.put("Unsigned", el.getChild("Unsigned").getText());
			map.put("EmpID", el.getChild("EmpID").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("Title", el.getChild("Title").getText());
			map.put("Content", el.getChild("Content").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			map.put("CreateDate", el.getChild("CreateDate").getText());
			map.put("KeepFlag", el.getChild("KeepFlag").getText());
			map.put("SugFlag", el.getChild("SugFlag").getText());
			map.put("TelNum", el.getChild("TelNum").getText());
			map.put("StatusCD", el.getChild("StatusCD").getText());
			map.put("SearchAuth", el.getChild("SearchAuth").getText());
		}
		// ---------------------------------------------------------------------
		// 공동실시자 분리
		String perStr = "";
		if (strJoinerArr != null && !"".equals(strJoinerArr[0])) {
			for (int i=0, len=strJoinerArr.length; i<len; i++) {
				String[] per = strJoinerArr[i].split(",");
				perStr += per[1]+"("+per[2]+") ";
			}
		}
		map.put("perStr", perStr);
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		modelAndView.addObject("page", page);
		String edtChk = "N";
		// 글내용 중 태그 존재시 (PC에서 에디터를 활용해서 등록한 경우 체크)
		if (((String)map.get("Content")).indexOf("<html>") != -1) {
			edtChk = "Y";
		}
		modelAndView.addObject("edtChk", edtChk);
		return modelAndView;
	}

}

